#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;	
int c, T;
const int N = 2010;
const int Mod = 998244353;
vector<int>v[N];
int dis[N][N], vis[N];
priority_queue<pii, vector<pii>, greater<pii> >q;
struct edge {
	int x, y;
}in[N * N];
signed main() {
	freopen("oi.in", "r", stdin);
	freopen("oi.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> c >> T;
	int num = 0;
	while(T--) {
		++num;
		int n, m;
		cin >> n >> m;
		for(int i = 1; i <= n; i++) v[i].clear();
		for(int i = 1; i <= m; i++) {
			int x, y;
			cin >> x >> y;
			in[i].x = x, in[i].y = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		if(3 <= c && c <= 4) {
			cout << -(m - 1) * 2 << " " << 666 << endl;
			continue;
 		}
		int ans = 1e18;
		for(int t = 1; t <= m; t++) {
			int i = in[t].x;
			for(int j = 1; j <= n; j++) dis[i][j] = 1e18, vis[j] = 0;
			dis[i][i] = 0;
			
			q.push(make_pair(0, i));
			while(!q.empty()) {
				pair<int,int> p = q.top();
				q.pop();
				if(p.second == in[t].y) break;
				if(vis[p.second]) continue;
				vis[p.second] = 1;
				for(int j = 0; j < v[p.second].size(); j++) {
					int y = v[p.second][j];
					if(p.second == in[t].x && y == in[t].y || p.second == in[t].y && y == in[t].x) continue;
					if(dis[i][y] > dis[i][p.second] + 1) {
						dis[i][y] = dis[i][p.second] + 1;
						q.push(make_pair(dis[i][y], y));
					}
				}
			}
			ans = min(ans, dis[in[t].x][in[t].y]);
		}
		cout << -(m - 1 + ans) << " " << 666 << endl;
	}
	return 0;
}
/*
1 1
5 5
1 2
1 3
2 5
3 4
4 5
*/
